#!/usr/bin/env python
# coding=utf-8


import numpy as np
from sklearn import mixture
import joblib
import os
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--trials_path", type=str, default="trials", help="trial path")
parser.add_argument("--enroll-dir", type=str, default="enroll", help="enroll path")
parser.add_argument("--test-dir", type=str, default="test", help="test path")
parser.add_argument("--gmm-type", type=str, default="diag", help="gmm type")
parser.add_argument("--score-log", type=str, default="score.log", help="score")
parser.add_argument("--n-components", type=int, default=8, help="score")
args = parser.parse_args()

trials = np.loadtxt(args.trials_path, dtype=str)

with open(args.score_log, 'w') as f:
	curent = None
	for idx in range(len(trials)):
		try:
			if curent != trials[idx][0]:
				print("loading {} gmm model...".format(trials[idx][0]))
				gmm = joblib.load(os.path.join(args.enroll_dir, trials[idx][0], "gmm_"+str(args.n_components)+"."+args.gmm_type))
				curent = trials[idx][0]
			test_data = np.load(os.path.join(args.test_dir, trials[idx][1], "dvector.npy"))
			score = gmm.score(test_data)
			if idx == 1000:
				print("{}\t{}\t{}\t{}".format(trials[idx][0], trials[idx][1], score, trials[idx][2]))
			f.write("{} {}\n".format(score, trials[idx][2]))
		except:
			pass

